<?php


namespace app\reception\controller;


use app\model\Friend;
use app\model\Users;

class User {
	function user_list() {
		$input = input('post.');
		$user = Users::where(['account' => $input['account']])->select();
		return json([$user]);
	}

	function friend_list() {
		$input = input('get.');
		$user = Friend::where(['users_id' => $input['id']])->select();
		return json([$user]);
	}

	//修改用户信息
	function user_update() {
		$input = input('post.');
		$user = Users::where(['id' => $input['id']])->update([
			'name' => $input['name'],
			'address' => $input['name'],
			'password' => $input['name'],
		]);
		return json([$user]);
	}

	//上传头像
	function user_headimg() {
		$input = input('post.');
		//判断文件上传是否出错
		if ($_FILES["file"]["error"]) {
			echo $_FILES["file"]["erroe"];
		} else {
			//控制上传的文件类型，大小
			if ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/png" && $_FILES["file"]["size"] < 1024000) {
				preg_match("/\/(.*)/", $_FILES["file"]["type"], $checkUrl);
				$name = time();
				$dir = "static/img/" . $name;
				$filename = $dir . "." . $checkUrl['1'];
				if (!file_exists($dir)) {
					mkdir($dir, 0777);
				}
				move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
				Users::where(['id' => $input['id']])->update(['headimg' => $name . "." . $checkUrl['1']]);
				return json(['state' => true, 'msg' => "上传成功！"]);
			} else {
				return json(['state' => false, 'msg' => "文件类型不正确！"]);
			}
		}
	}
}